﻿/* Google Maps API */
var map;
var markersArray = [];
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();

$(window).resize(function () {
    mapSize();
});

function mapSize() {
    $("#map").width(
        //$("#main").outerWidth() - $("#sidebar").outerWidth() - ($("#content").outerWidth() - $("#content").width()) - 20
    );
}

function init() {
    mapSize();

    var mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(40.7497215270996, -96.701545715332),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map"), mapOptions);

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
            map.setZoom(11);
        });
    }
}
google.maps.event.addDomListener(window, 'load', init);

function addMarker(lat, lng, title, id) {
    var position = new google.maps.LatLng(lat, lng);
    //Create Marker and add to Map
    marker = new google.maps.Marker({
        position: position,
        map: map,
        title: title
    });

    bounds.extend(position);
    map.fitBounds(bounds);

    //Add Click Event to show details
    google.maps.event.addListener(marker, "click", function () {
        if ($("#user-rentals").is(":visible")) {
            $("div#rental-details").hide();
            //above probably won't work on every browser, so leaving old working code
            //$("div#rental-details").each(function () { $(this).hide(); });
            $("#user-rental[class='" + id + "'] #rental-details").show();
        }
        else {
            infowindow.setContent(title);
            infowindow.open(map, this);
        }
    });

    markersArray.push(marker);
}

// Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
    }
}

// Shows any overlays currently in the array
function showOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(map);
        }
    }
}

// Deletes all markers in the array by removing references to them
function deleteOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }
}